import Taro, { uploadFile } from "@tarojs/taro";
const CONFIG = require('../config')

export interface Options {
    method : 'POST' | 'GET' | 'DELETE';
    data? : any;
}

export interface Result {
    success : boolean;
    code? : number;
    data? : any;
    msg : string;
}

class Http {
    private sending:boolean = false;

    async post(url: string, data?: any)  : Promise<Result>{
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.post(url, data)
        this.sending = false
        return result
    }

    static post(url: string, data?: any) : Promise<Result> {
        let options:Options = {
            method : 'POST'
        }
        if(data) {
            options.data = data
        }

        return Http.send(url, options)
    }

    async get(url: string, data?: any) : Promise<Result>{
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.get(url, data)
        this.sending = false
        return result
    }

    static get(url: string, data?: any) : Promise<Result>{
        let options:Options = {
            method : 'GET'
        }

        if(data) {
            options.data = data
        }

        return Http.send(url, options)
    }

    async delete(url: string, data?: any) : Promise<Result>{
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.delete(url, data)
        this.sending = false
        return result
    }

    static delete(url: string, data?: any) : Promise<Result>{
        let options:Options = {
            method : 'DELETE'
        }

        if(data) {
            options.data = data
        }

        return Http.send(url, options)
    }

    static header() {
        let header:{[key : string] : string | number} = {

        }

        if(process.env.access_token) {
            header.Authorization = process.env.access_token
        }

        return header
    }

    static responseCallback(data) {
        let result:Result = {
            msg : '',
            success : false
        }

        if(data) {
            result.code = data.code
            result.data = data.data
            result.msg = data.msg
            result.success = data.code == 200 || data.code == 0
        }

        return result
    }

    static async send(url: string, options: Options) : Promise<Result>{
        try {

            let header = Http.header()
            header['Content-type'] = 'application/json'


            let response = await Taro.request({
                url : CONFIG.API_URL + url,
                ...options,
                header,
                dataType : 'json'
            })
            
            return Http.responseCallback(response.data)

        } catch (error) {console.log(error)
            return {
                msg : error.message,
                success : false
            } as Result
        }

        
    }


    static async uploadFile(url:string, filePath:string, name:string = 'file', formData?:{[key:string]:any}) {
        try{
          let option:uploadFile.Option = {
              url : CONFIG.API_URL + url,
              filePath,
              name,
              fileName : name,
              header : Http.header()
          }
          if(formData) {
              option.formData = formData
          }
          let response = await Taro.uploadFile(option)
          return Http.responseCallback(JSON.parse(response.data))
        }catch(error){
            return {
                msg : error.message,
                success : false
            } as Result
        }
    }



}

export default Http
